package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.http.LoginHttp;
import shuashuami.hb.com.share.ShareCUserInfo;

public class AChangePasswordActivity extends AbActivity {

    private EditText edtOld, edtNew, edtEnter;
    private Button btnSave;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_change_password);
        edtOld = (EditText) findViewById(R.id.edt_change_password_old);
        edtNew = (EditText) findViewById(R.id.edt_change_password_new);
        edtEnter = (EditText) findViewById(R.id.edt_change_password_enter);
        btnSave = (Button) findViewById(R.id.btn_change_password_enter);
    }

    @Override
    protected void initData() {
        setTitleBar("修改密码");
        setLeftView();
    }

    @Override
    protected void initListener() {
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changePassword();
            }
        });
    }

    private void changePassword() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpMethods.changePassword(listener, shareCUserInfo.getId(), edtOld.getText().toString(), edtNew.getText().toString(), edtEnter.getText().toString()
        );
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(AChangePasswordActivity.this);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) { //成功
                            ToastUtil.showShort(AChangePasswordActivity.this, jsonObject.getString("message"));
                            clear();
                            openActivity(HbLoginAct.class);

                            finishAllWithOutLast();
                            finish();
                        } else {
                            ToastUtil.showShort(AChangePasswordActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(AChangePasswordActivity.this, "数据解析错误");
                    }


                    break;
            }

            super.handleMessage(msg);
        }
    };
    private void clear() {
        ShareCUserInfo shareCUserInfo  = new ShareCUserInfo(this);
        shareCUserInfo.clear();
    }
}
